package designPatterns.structureMode.decorator;

/**
 * Created by thinkpad on 2015/8/15.
 * Decorator pattern adds additional features to an existing object dynamically.
 * In this post, I will use a simple example - decorate your girlfriend -
 * to illustrate how decorator pattern works.

 1. Decorator Pattern Story

 Let's assume you are looking for a girlfriend.
 There are girls from different countries such as America, China, Japan, France,
 etc. They may have different personalities and hobbies.
 In a dating web like eharmony.com,
 if each type of girl is an individual Java class,
 there would be thousands of classes.
 That is a serious problem called class explosion.
 Moreover, this design is not extensible.
 Whenever there is a new girl type, a new class needs to be created.

 Let's change the design, and let each hobby/personality becomes a decorator which can be dynamically applied to a girl.

 */
//Girl is the abstract class at the top level, we have girls from different countries. With a GirlDecorator class, we can decorator each girl with any feature by adding a new decorator.
abstract class Girl {
    String description = "no particular";

    public String getDescription(){
        return description;
    }
}

class AmericanGirl extends Girl {
    public AmericanGirl(){
        description = "+American";
    }
}

class EuropeanGirl extends Girl {
    public EuropeanGirl() {
        description = "+European";
    }
}

abstract class GirlDecorator extends Girl {
    public abstract String getDescription();
}

class Science extends GirlDecorator {

    private Girl girl;

    public Science(Girl g) {
        girl = g;
    }

    @Override
    public String getDescription() {
        return girl.getDescription() + "+Like Science";
    }

    public void caltulateStuff() {
        System.out.println("scientific calculation!");
    }
}
class Art extends GirlDecorator {

    private Girl girl;

    public Art(Girl g) {
        girl = g;
    }

    @Override
    public String getDescription() {
        return girl.getDescription() + "+Like Art";
    }

    public void draw() {
        System.out.println("draw pictures!");
    }
}

class Main {

    public static void main(String[] args) {
        Girl g1 = new AmericanGirl();
        System.out.println(g1.getDescription());

        Science g2 = new Science(g1);
        System.out.println(g2.getDescription());

        Art g3 = new Art(g2);
        System.out.println(g3.getDescription());
    }
}


